package com.retallife.business.controller;


import com.retallife.business.exception.Result;
import com.retallife.business.pojo.Complaint.UserComplaintVo;
import com.retallife.business.service.Complaint.ComplaintService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "用户投诉类")
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET})
public class BusinessComplaintController {

    @Autowired
    ComplaintService complaintService;

    //Email所需要的类
    @Autowired
    JavaMailSenderImpl mailSender;

    @ApiOperation(value = "查询所有用户的投诉信息")
    @RequestMapping("BusinessFindComplaintAll")
    public List<UserComplaintVo> BusinessFindComplaintAll() {
        return complaintService.AdminFindComplaintAll();
    }

    @ApiOperation(value = "查询某个用户的投诉信息")
    @RequestMapping("BusinessFindComplaints")
    public List<UserComplaintVo> BusinessFindComplaints(@RequestParam String userComplaintName) {
        return complaintService.AdminFindComplaints(userComplaintName);
    }

    @ApiOperation(value = "回复用户提交的投诉信息")
    @RequestMapping("BusinessUpdateComplaints")
        public Result BusinessUpdateComplaints(@RequestParam String userComplaintName, String Email, String Content){
        int result = complaintService.AdminUpdateComplaints(userComplaintName);
        //一个简单的邮件
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //标题
        mailMessage.setSubject("RetalLife管理员的回复");
        //内容
        mailMessage.setText(Content);
        //发给谁？
        mailMessage.setTo(Email);
        //谁发送？
        mailMessage.setFrom("wsw1335860166@163.com");
        //信息模板
        mailSender.send(mailMessage);
        if (result > 0){
            return Result.ok("回复成功");
        }
        return Result.ok("回复失败");
    }

}
